import {
  defineStore
} from 'pinia'
export const goodsStore = defineStore('goods', {
  state: () => {
    return {
      //首页商品列表
      homegoods: {
        status: "idle",
        //写意生活数据
        xiye: [],
        //热门推荐数据
        hotlist: [],
        //人气热卖数据
        porhotlist: [],
        error: ""
      },
      //秒杀数据
      miaoshagoods: {
        status: "idle",
        endmiaoshalist: [], //已经结束的秒杀数据
        miaoshalist: [], //秒杀数据
        wksmiaoshalist: [], //未开始的秒杀数据
        timestamp: 0, //秒杀时间
        startimestamp: 0, //距离秒杀开始时间
        kanjialist: [],
        error: ""
      },
      //拼团数据和砍价数据
      ptkjlist: {
        status: "idle",
        //砍价数据
        kanjialist: [],
        //拼团数据
        pintuanlinst: [],
        error: ""
      },
      //砍价详情
      kanjiaxinxi: {
        status: "idle",
        timestamp: 0,
        num: 0,
        kanjiaxg: {
          is_Kanjia: false, //是否发起砍价
          kanj_count: 0,
          price: 0,
          is_Wancheng: false, //是否完成砍价
          percent: "",
        },
        error: "",

      },
      //拼团详情
      pintuanxinxi:{
            status: "idle",
             timestamp: 0,
             pintuanxg: {
               is_Pint: false, //是否发起拼团
               is_Ptwc: false, //是否完成拼团
                is_auto:false,//自己还未参与
                group_size:0,//参与人数
                group_all:0,//共需人数
               percent: "",
             },
      },
      //品牌分类列表
      brandlist: {
        status: "idle",
        list: [],
        error: ""
      },
      //商品详情
      goods_Detail: {
        status: "idle",
        error: "",
        //定义商品详情数据
        goods_Detailst: {
          name: "", //商品名称
          unit_price: "", //商品价格
          discount_price: "", //商品打折价格
          goodswep: "", //商品轮播图
          goodsfigure: "", //商品详情轮播图
          sold_count: "", //已售,
          all_count: "" //全部
        },
        //定义商品评论列表
        goods_comment: {
          username: "", //用户名
          avatar: "", //用户头像
          rating: "", //评论评分
          content: "", //评论内容
          created_time: "", //评论时间
          CommetImg: "", //配图组
        }
      }

    }

  },
  actions: {
    //获取首页数据
    async getindexgoods() {
      //更改请求状态
      this.homegoods.status = "padding"
      try {
        const response = await uni.$http.get("/goods/index")
        this.homegoods.status = "success"
        this.homegoods.xiye = response.data.xieyi,
          this.homegoods.hotlist = response.data.hotlist,
          this.homegoods.porhotlist = response.data.porhotlist
      } catch (error) {
        this.homegoods.status = "error"
        this.homegoods.error = error
      }
    },
    //获取秒杀数据
    async getmiaoshagoods() {
      //更改请求状态
      this.miaoshagoods.status = "padding"
      try {
        const response = await uni.$http.get("/goods/miaosha")
        this.miaoshagoods.status = "success"
        this.miaoshagoods.miaoshalist = response.data.miaosha
        this.miaoshagoods.endmiaoshalist = response.data.endmiaosha
        this.miaoshagoods.wksmiaoshalist = response.data.wksmiaosha
        this.miaoshagoods.startimestamp = response.data.startimestamp
        this.miaoshagoods.timestamp = response.data.timestamp
      } catch (error) {
        this.miaoshagoods.status = "error"
        this.miaoshagoods.error = error
      }
    },
    //获取拼团数据和砍价数据
    async getptkjlist() {
      //更改请求状态
      this.ptkjlist.status = "padding"
      try {
        const response = await uni.$http.get("/goods/getptkj")
        this.ptkjlist.status = "success"
        this.ptkjlist.kanjialist = response.data.kanjialist
        this.ptkjlist.pintuanlinst = response.data.pintuanlinst
      } catch (error) {
        this.ptkjlist.status = "error"
        this.ptkjlist.error = error
      }
    },
    //获取分类数据
    async getbrand() {
      //更改请求状态
      this.brandlist.status = "padding"
      // 发送请求
      try {
        const response = await uni.$http.get("/goods/brand")
        this.brandlist.status = "success"
        this.brandlist.list = response.data
      } catch (error) {
        this.brandlist.status = "error"
        this.brandlist.error = error
      }
    },
    //获取商品详情
    //定义获取商品详情和商品评论的函数
    async getgoodsDetail(goods_id) {
      //更改请求状态
      this.goods_Detail.status = "padding"
      try {
        const reponse = await uni.$http.get('/goods/detail', {
          goods_id
        })
        //更改请求状态
        this.goods_Detail.status = "success"
        this.goods_Detail.goods_Detailst = reponse.data.product_detail,
          this.goods_Detail.goods_comment = reponse.data.comentlist
      } catch (error) {
        this.goods_Detail.status = "error"
        this.goods_Detail.error = error
      }

    },
    //判断是否正在砍价
    async is_Kanjia(goods_id) {
      this.kanjiaxinxi.status = "padding"
      try {
        const reponse = await uni.$http.post('/goods/iskanjia', {
          goods_id
        })
        this.kanjiaxinxi.timestamp = reponse.data.timestamp
        this.kanjiaxinxi.kanjiaxg = reponse.data.kanjiaxg
        this.kanjiaxinxi.status = "success"
      } catch (error) {
        this.goods_Detail.status = "error"
        this.goods_Detail.status = error
      }

    },
    //发起砍价
    async Faqikanjia(goods_id) {
      const reponse = await uni.$http.post('/goods/faqikj', {
        goods_id
      })
      uni.showToast({
        title: "发起砍价成功",
        duration: 2000,
        icon: "success"
      });
      this.kanjiaxinxi.timestamp = reponse.data.timestamp
      this.kanjiaxinxi.kanjiaxg = reponse.data.kanjiaxg
      this.kanjiaxinxi.status = "success"
    },
    //砍一刀
    async kanyidao(goods_id) {
      const reponse = await uni.$http.post('/goods/kanyidao', {
        goods_id
      })
     
      this.kanjiaxinxi.num = reponse.data.num
      this.kanjiaxinxi.kanjiaxg = reponse.data.kanjiaxg
      this.kanjiaxinxi.status = "success"
    },
  //是否正在拼团
    
  async is_Pintuan(goods_id){
    const reponse = await uni.$http.post('/goods/ispintuan', {
      goods_id
    })
    // console.log(reponse);
    this.pintuanxinxi.status = "success"
      this.pintuanxinxi.timestamp = reponse.data.timestamp
    this.pintuanxinxi.pintuanxg = reponse.data.pintuanxg
    
  },
  //发起拼团
  async kaituan(goods_id) {
    try{
       this.pintuanxinxi.status = "padding"
      const reponse = await uni.$http.post('/goods/kaituan', {
        goods_id
      })
      if(reponse.data.pintuanxg.group_size===1){
        uni.showToast({
          title: '开团成功',
          duration: 2000,
          icon: "success"
        });
      }else{
        uni.showToast({
          title: '成功加入团购',
          duration: 2000,
          icon: "success"
        });
      }
      this.pintuanxinxi.status = "success"
      this.pintuanxinxi.timestamp = reponse.data.timestamp
      this.pintuanxinxi.pintuanxg = reponse.data.pintuanxg
    }catch(e){
      this.pintuanxinxi.status = "error"
      this.pintuanxinxi.error = error
    }
  
    
  },
  },

})